<?php
namespace common\services\auth;
use linslin\yii2\curl\Curl;

use Yii;
/**
 * Created by PhpStorm.
 * User: eycuit
 * Date: 2020/2/27
 * Time: 15:14
 */

class AuthApiAliyun {

    public $secretId = 'AKIDNmxsa3KH9nLehWDPkpjNq5NRQ9boDpeE';
    public $secretKey = 'Vt3way6ycDn7E7k8IbfXCaj39zUq69yZ';

    /**
     * 身份证二要素认证
     * 参考地址 https://market.aliyun.com/products/57000002/cmapi00037883.html?spm=5176.11065268.1996646101.searchclickresult.24235676EZIjty#sku=yuncode3188300002
     * @param string $name
     * @param string $idNumber
     *
     * @return mixed
     */
    public static function authIdentity($name = '', $idNumber = '') {

        $apiUrl = 'https://zid.market.alicloudapi.com/idcard/VerifyIdcardv2';

        $AppCode =  Yii::$app->params['ALIYUN_MARKET_APP_CODE'];

        $curl = new Curl();

        $curl->setHeader('Authorization', "APPCODE " . $AppCode);
//        $params['realName'] = urlencode($name);
        $params['realName'] = $name;
        $params['cardNo'] = $idNumber;
        $curl->setGetParams($params);
        $res = $curl->get($apiUrl);
        return json_decode($res, true);
    }

    /**
     * 银行卡三要素认证
     * 参考地址 https://market.aliyun.com/products/57000002/cmapi033466.html?spm=5176.730005.productlist.d_cmapi033466.369d3524yVdNMt&innerSource=search_%E9%93%B6%E8%A1%8C%E5%8D%A1%E4%B8%89%E8%A6%81%E7%B4%A0#sku=yuncode2746600001
     * @param string $name
     * @param string $cardNo
     * @param string $idNumber
     *
     * @return mixed
     */
    public static function authBank($name = '', $idNumber = '', $cardNo = '') {

        $apiUrl = 'https://bankcard3c.shumaidata.com/bankcard3c';

        $AppCode =  Yii::$app->params['ALIYUN_MARKET_APP_CODE'];

        $curl = new Curl();

        $curl->setHeader('Authorization', "APPCODE " . $AppCode);

        $params['bankcard'] = $cardNo;
        $params['name'] = $name;
        $params['idcard'] = $idNumber;

        $curl->setGetParams($params);
        $res = $curl->get($apiUrl);
        return json_decode($res, true);
    }

    public static function queryBankInfo($cardNo = '') {
        //https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo=银行卡卡号&cardBinCheck=true

        $url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardNo={$cardNo}&cardBinCheck=true";

        $curl = new Curl();

//        $params['_input_charset'] = 'utf-8';
//        $params['cardNo'] = $cardNo;
//        $params['cardBinCheck'] = true;
//        $curl->setGetParams($params);

        $res = $curl->get($url);
        return json_decode($res, true);

    }

    /**
     * 身份证+名字+活体认证
     * 参考地址 https://market.aliyun.com/products/57124001/cmapi033789.html?spm=5176.product-detail.sidebar.2.70615e60H0AOFC&scm=20140722.C_cmapi033789.P_146.MO_732-ST_4769-V_1-ID_cmapi033789-OR_rec#sku=yuncode2778900001
     * @param string $name
     * @param string $idNumber
     *
     * @return mixed
     */
    public function idFaceIdentity($name = '', $idNumber = '', $base64Str = '') {

        $apiUrl = 'https://faceidcardb.shumaidata.com/getfaceidb';

        $AppCode = \Yii::$app->params['ALIYUN_MARKET_APP_CODE'];

        $curl = new Curl();

        $curl->setHeader('Authorization', "APPCODE " . $AppCode);
        $params['image'] = trim($base64Str);
        $params['name'] = $name;
        $params['idcard'] = $idNumber;
        $curl->setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        $curl->setPostParams($params);

        $res = $curl->post($apiUrl);
        return json_decode($res, true);
    }
    /**
     * 身份证+名字+活体认证
     * 参考地址 https://market.aliyun.com/products/57126001/cmapi00038275.html?spm=5176.11065268.1996646101.searchclickresult.1ba88cf4c7gGat#sku=yuncode3227500002
     * @param string $name
     * @param string $idNumber
     *
     * @return mixed
     */
    public function setPhone($mobile = '') {

        $apiUrl = 'https://api04.aliyun.venuscn.com/mobile';

        $AppCode = \Yii::$app->params['ALIYUN_MARKET_APP_CODE'];

        $curl = new Curl();

        $curl->setHeader('Authorization', "APPCODE " . $AppCode);
        $params['mobile'] = trim($mobile);
        $curl->setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        $curl->setGetParams($params);

        $res = $curl->get($apiUrl);

        return json_decode($res, true);
    }

    public static function verifyPhoneUmeng($token = '', $phone = 0) {
        $url = "https://verify5.market.alicloudapi.com/api/v1/mobile/verify?appkey=61231eb05358984f59b2aec1&verifyId=";

        $curl = new Curl();

        $body['token'] = $token;
        $body['phoneNumber'] = $phone;
        $time = function() {
            list($s1, $s2) = explode(' ', microtime());
            return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        };
        $headers = [
            //请求体类型。
            "Content-Type" => "application/json; charset=UTF-8",
            //请求响应体类型，部分 API 可以根据指定的响应类型来返回对应数据格式，建议手动指定此请求头，如果不设置，部分 HTTP 客户端会设置默认值 */*，导致签名错误。
            "Accept" => "application/json",
            // API 版本号，目前所有 API 仅支持版本号『1』，可以不设置此请求头，默认版本号为『1』。
            "X-Ca-Version" => 1,
            //参与签名的自定义请求头，服务端将根据此配置读取请求头进行签名，此处设置不包含 Content-Type、Accept、Content-MD5、Date 请求头，这些请求头已经包含在了基础的签名结构中，详情参照请求签名说明文档。
            "X-Ca-Signature-Headers" => "X-Ca-Request-Mode,X-Ca-Version,X-Ca-Stage,X-Ca-Key,X-Ca-Timestamp",
            "X-Ca-Stage" => "RELEASE",
            //请求的 阿里云AppKey，通过云市场等渠道购买的 API 默认已经给 APP 授过权，阿里云所有云产品共用一套 AppKey 体系，删除 ApppKey 请谨慎，避免影响到其他已经开通服务的云产品。
            "X-Ca-Key" =>  203980382,
            //请求的时间戳，值为当前时间的毫秒数，也就是从1970年1月1日起至今的时间转换为毫秒，时间戳有效时间为15分钟。
            "X-Ca-Timestamp" => $time,
            //请求唯一标识，15分钟内 AppKey+API+Nonce 不能重复，与时间戳结合使用才能起到防重放作用。
            "X-Ca-Nonce" => md5(203980382 . $phone),
            //请求签名,使用方参照签名机制生成。
            "X-Ca-Signature" => "FJleSrCYPGCU7dMlLTG+UD3Bc5Elh3TV3CWHtSKh1Ys="
        ];

        $curl->setHeaders($headers);
        $curl->setRequestBody(json_encode($body));

        $res = $curl->post($url);
        var_dump($res);die;
        return json_decode($res, true);

    }

}